package com.systemic.controller;

import com.systemic.core.ResponseResult;

import com.systemic.pojo.Friend;
import com.systemic.pojo.FriendApply;
import com.systemic.pojo.User;
import com.systemic.pojo.UserInfo;
import com.systemic.service.FriendApplyService;
import com.systemic.service.FriendService;
import com.systemic.service.UserInfoService;
import com.systemic.service.UserService;
import com.systemic.vo.Contact;
import com.systemic.vo.FriendInfo;
import com.systemic.vo.SearchUser;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: FriendController
 * @Description: 好友模块控制层
 * @Author:
 * @CreateTime: 2023-12-28 17:35
 * @Version:
 **/

@RestController
@RequestMapping("/friend")
public class FriendController {

    @Resource
    private UserService userService;

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private FriendService friendService;

    @Resource
    private FriendApplyService friendApplyService;

    /**
     * 获取用户列表(搜索用户)
     * @param userInfo
     * @return
     */
    @PostMapping("/searchList")
    public ResponseResult<List<SearchUser>> searchFriendList(@RequestBody UserInfo userInfo) {
        List<UserInfo> userInfoList = userInfoService.select(userInfo);
        if (userInfoList != null) {
            List<SearchUser> searchUserList = new ArrayList<>();
            for (UserInfo info : userInfoList) {
                int num = 0;
                SearchUser searchUser = new SearchUser();
                try {
                    searchUser.setUid(info.getUid());
                    searchUser.setUname(info.getUname());
                    searchUser.setEmail(info.getEmail());
                    searchUser.setGender(info.getGender());
                    searchUser.setBirthday(info.getBirthday());
                    searchUser.setAvatar(info.getAvatar());
                    searchUser.setArea(info.getArea());
                    searchUserList.add(searchUser);
                    num += 1;
                    System.out.println("查询到第" + num + "个用户");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return ResponseResult.success(searchUserList);
        } else {
            return ResponseResult.failed("没有查询到用户");
        }
    }

    /**
     * 搜索用户
     * @param user
     * @return
     */
    @PostMapping("/search")
    public ResponseResult<SearchUser> searchFriend(@RequestBody User user){
        String targetUserAccount =  user.getAccount();
        int currentUserId = user.getUid();
        User targetUser = userService.findByAccount(targetUserAccount);
        if (targetUser!=null){
            int targetUserId = targetUser.getUid();
            UserInfo info = userInfoService.selectUserInfoByUid(targetUserId);
            FriendApply friend = new FriendApply();
            friend.setUid(currentUserId);
            friend.setTarget(targetUserId);
            FriendApply friendApply = friendApplyService.select(friend);

            SearchUser res = new SearchUser();
            if (friendApply!=null){
                res.setStatus(friendApply.getStatus());
            } else {
                res.setStatus(0);
            }
            res.setUid(info.getUid());
            res.setUname(info.getUname());
            res.setEmail(info.getEmail());
            res.setGender(info.getGender());
            res.setBirthday(info.getBirthday());
            res.setAvatar(info.getAvatar());
            res.setArea(info.getArea());

            return ResponseResult.success(res);
        } else {
            return ResponseResult.failed("未查询到用户");
        }

    }

    /**
     * 好友列表
     * @param uid
     * @return
     */
    @GetMapping("/friendList/{uid}")
    public ResponseResult<List<Contact>> friendList(@PathVariable("uid") Integer uid){
        List<Contact> contactList = friendService.selectListById(uid);
        return ResponseResult.success(contactList);
    }

    /**
     * 查看好友详细信息
     * @param fid
     * @return
     */
    @GetMapping("/friendInfo/{fid}")
    public ResponseResult<FriendInfo> friendInfo(@PathVariable("fid") Integer fid){
        FriendInfo friendInfo = friendService.selectFriendInfoByFid(fid);
        return ResponseResult.success(friendInfo);

    }

    /**
     * 好友列表（根据昵称模糊查询）
     * @param nickname
     * @return
     */
    @GetMapping("/likeSelect/{nickname}")
    public ResponseResult<List<Contact>> likeSelectFriend(@PathVariable("nickname") String nickname){
        List<Contact> contactList = friendService.selectListByName(nickname);
        return ResponseResult.success(contactList);
    }

    /**
     * 修改好友昵称
     * @param friend
     * @return
     */
    @PostMapping("/modify")
    public ResponseResult<String> updateNickname(@RequestBody Friend friend){
        int row = friendService.updateNickname(friend);
        if (row==0){
            return ResponseResult.failed("修改失败");
        }
        return ResponseResult.success("修改成功");
    }

    /**
     * 删除好友
     * @param friend
     * @return
     */
    @PostMapping("/delete")
    public ResponseResult<String> deleteFriend(@RequestBody Friend friend){
        int row = friendService.delete(friend);
        if (row==0){
            return ResponseResult.failed("删除失败");
        }
        return ResponseResult.success("删除成功");
    }
}
